package org.itboys.core.controller.web;

import com.wordnik.swagger.annotations.Api;
import org.itboys.core.dto.order.SundayOrderDto;
import org.itboys.core.entity.mysql.SundayOrder;
import org.itboys.core.entity.mysql.SundayOrderScene;
import org.itboys.core.service.mysql.web.SundayOrderService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者：zzq
 * 时间：2018/10/26
 * 描述：订单
 */
@Controller
@RequestMapping("/sunday/web/order")
@Api(value = "order", description = "订单")
public class SundayOrderController extends BaseController {
    @Autowired
    private SundayOrderService orderService;

    /**
     * 主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model) {
        return "/sunday/order/index";
    }

    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) throws IOException {
        PageResult<SundayOrder> result = orderService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }


    /**
     * 再次向江南布衣发起订单
     *
     * @param request
     * @param response
     */
    @RequestMapping("/createOrder")
    public void createOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "orderId", required = true) Long orderId
    ) throws IOException {
        orderService.createOrder(orderId);
        ResponseMessage.success();
    }

    /**
     * 详情页
     *
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value = "id", required = true) Long id,
            Model model) throws IOException {

        SundayOrderDto order = orderService.findOne(id);
        model.addAttribute("order", order);

        return "/sunday/order/input";
    }

    /**
     * 分页查询订单场景
     *
     * @param request
     * @param response
     */
    @RequestMapping("/scene/select")
    public void scene_select(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        PageResult<SundayOrderScene> result = orderService.selectScene(request, orderId);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }


}
